package dwz.web.management;

import dwz.business.enums.PageTarget;
import dwz.business.website.Page;
import dwz.persistence.beans.UserInfo;
import dwz.web.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;

/**
 * Created by Aislli on 2016/11/17.
 */
@Controller
@RequestMapping("/TestController")
public class TestController extends BaseController {

    @RequestMapping("/toHello")
    public String toHello(Model model) {
        ArrayList<UserInfo> strings = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            strings.add(new UserInfo("name---"+i,i%2==0?"男":"女",10+i));
        }
        model.addAttribute("key1", "hello TestController!");
        model.addAttribute("key2", strings);
        return "management/test/list";
    }

    @RequestMapping("/add")
    public String add(Model model) {
        model.addAttribute("pageTargets", PageTarget.values());

        return "/management/test/add";
    }

    @RequestMapping(value = "/insert" ,method = RequestMethod.POST)
    public ModelAndView insert(String sex,String name) {
        System.out.println("sssssssssssssssss:"+sex+"--"+name);
        return ajaxDoneSuccess(getMessage("msg.operation.success"));
    }

    @RequestMapping("/edit/{userName}")
    public String edit(@PathVariable("userName") String userName, Model model){
        System.out.println("sssssssssssssssss:"+userName);
        model.addAttribute("userInfo", new UserInfo(userName, "男", 12));
        return "/management/test/edit";
    }

    /*@RequestMapping(value = "/update", method = RequestMethod.POST)
    public ModelAndView update(UserInfo userInfo) {
        System.out.println("=====================================");
        System.out.println(userInfo.getUserName());
        System.out.println(userInfo.getSex());
        System.out.println(userInfo.getAge());
        System.out.println("=====================================");
        return ajaxDoneSuccess(getMessage("msg.operation.success"));
    }*/
    @RequestMapping("/update")
    public ModelAndView update(UserInfo userInfo) {
        System.out.println("=====================================");
        System.out.println(userInfo.getUserName());
        System.out.println(userInfo.getSex());
        System.out.println(userInfo.getAge());
        System.out.println("=====================================");
        return ajaxDoneSuccess(getMessage("msg.operation.success"));
    }
}
